<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class User extends AdminController{
    public $_validation = null;

    function __construct(){
        parent::__construct();
        $this->load->model('MUser');
        $this->_validation = Array(
            array('field'=>'user_type', 'label'=>'用户类型', 'rules'=>'required|max_length[1]')
            ,array('field'=>'wx_bind', 'label'=>'绑定微信状态', 'rules'=>'max_length[1]')
            ,array('field'=>'auth_status', 'label'=>'认证状态', 'rules'=>'max_length[1]')
            ,array('field'=>'open_id', 'label'=>'微信openID', 'rules'=>'max_length[64]')
            ,array('field'=>'session_key', 'label'=>'会话标识', 'rules'=>'max_length[64]')
            ,array('field'=>'mobile', 'label'=>'手机号', 'rules'=>'max_length[64]')
            ,array('field'=>'nick_name', 'label'=>'微信昵称', 'rules'=>'max_length[64]')
            ,array('field'=>'sex', 'label'=>'性别', 'rules'=>'integer')
            ,array('field'=>'province', 'label'=>'省份', 'rules'=>'max_length[32]')
            ,array('field'=>'city', 'label'=>'城市', 'rules'=>'max_length[32]')
            ,array('field'=>'avatar', 'label'=>'微信头像', 'rules'=>'max_length[255]')
            ,array('field'=>'login_ip', 'label'=>'登录ip', 'rules'=>'max_length[64]')
            ,array('field'=>'login_time', 'label'=>'上次登陆时间', 'rules'=>'integer')
            ,array('field'=>'create_time', 'label'=>'创建时间', 'rules'=>'integer')
            ,array('field'=>'update_time', 'label' => '更新时间', 'rules' => 'integer')
        );
    }

    function index(){
        //$this->output->enable_profiler(TRUE);
        $this->load->library('FormFilter');
        $this->load->helper('formfilter');
        $this->formfilter->addFilter('user_type', 'where');
        $this->formfilter->addFilter('wx_bind', 'where');
        $this->formfilter->addFilter('auth_status', 'where');
        $this->formfilter->addFilter('open_id', 'where');
        $this->formfilter->addFilter('mobile', 'where');
        $this->formfilter->addFilter('nick_name', 'like');
        $this->formfilter->addFilter('sex', 'where');

        if ($begintime = filterValue('user_create_time_begin')) {
            $this->formfilter->addFilter('user_create_time_begin', 'where', array('create_time >= ', strtotime($begintime . ' 00:00:00')));
        }
        if ($endtime = filterValue('user_create_time_end')) {
            $this->formfilter->addFilter('user_create_time_end', 'where', array('create_time <= ', strtotime($endtime . ' 23:59:59')));
        }
        $number_perpage = filterValue('user_number_perpage');
        $total_number = $this->MUser->getCount();
        $limit = $this->pagination($total_number,'',$number_perpage);
        $list  = $this->MUser->getList($limit);
        $this->assign('list', $list);
        $this->layout();
    }

    public function change_user(){
        $data = $this->input->post();
        if ($this->db->update('user',['user_type' => $data['user_type']],['id' => $data['id']])){
            exit(json_encode([
                'status' => 1,
                'msg' => '操作成功'
            ]));
        }else{
            exit(json_encode([
                'status' => 0,
                'msg' => '操作失败'
            ]));
        }
    }
}
